package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.ChapterMapper;
import com.atguigu.eduservice.service.ChapterService;
import com.atguigu.eduservice.service.VideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author AIR
 * @since 2022-09-20
 */
@Service
@Transactional
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, EduChapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseID) {
        //根据课程id查章节
        LambdaQueryWrapper<EduChapter> wrapperChapter = new LambdaQueryWrapper<>();
        wrapperChapter.eq(EduChapter::getCourseId,courseID);
        wrapperChapter.eq(EduChapter::getCourseId, courseID);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapperChapter);

        //根据课程id查小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseID);
        List<EduVideo> eduVideos = videoService.list(wrapperVideo);

        //创建一个最终集合存储EduChapter内容
        List<ChapterVo> finalList = new ArrayList<>();

        for (int i = 0; i < eduChapters.size(); i++) {
            //创建ChapterVo对象存储内容
            ChapterVo chapterVo = new ChapterVo();
            //遍历所有章节
            EduChapter eduChapter = eduChapters.get(i);
            //根据属性赋值到chapterVo中去
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finalList.add(chapterVo);

            //创建集合，用于封装章节小节
            List<VideoVo> videoVoList = new ArrayList<>();
            for (int m = 0; m < eduVideos.size(); m++) {
                //创建Vi  deoVo对象存储内容
                VideoVo videoVo = new VideoVo();
                //遍历所有小节
                EduVideo eduVideo = eduVideos.get(m);
                //根据小节里的chapterId等于章节id
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }

        return finalList;
    }

    @Override
    public Boolean deleteChapter(String chapterId) {
        //根据chapterid查询章节，如何有小节表，不进行删除
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("chapter_id", chapterId);
        long count = videoService.count(wrapperVideo);
        System.out.println(count);
        if (count > 0) {//查询出小节，不进行删除
            throw new GuliException(20001, "删除章节，要先删除小节");
        } else {//不能查询数据，进行删除
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            //成功 1>0
            return result > 0;
        }
    }

    //2 根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        baseMapper.delete(wrapperChapter);
    }
}
